package com.hr.runnerbuddy;

import java.util.ArrayList;
import java.util.List;

import com.hr.runnerbuddy.database.DBAdapter;
import com.hr.runnerbuddy.database.LoopsessiePunt;
import com.hr.runnerbuddy.database.ParcourPunt;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Resultaten extends Activity {

	private DBAdapter dbHelper;
	private int countDB;
	private List<LoopsessiePunt> loopsessiePunt = new ArrayList<LoopsessiePunt>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		dbHelper = new DBAdapter(getApplicationContext());
		dbHelper.open();

		if (dbHelper.getAllLoopsessiePunten(0) != null) {
			countDB = dbHelper.getAllLoopsessiePunten(0).size();

			TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayoutR);
			TableRow[] tableRow = new TableRow[countDB];
			loopsessiePunt = dbHelper.getAllLoopsessiePunten(0);
			if (loopsessiePunt != null) {
				for (int i = 0; i < countDB; i++) {
					tableRow[i] = new TableRow(this);
					TextView textView1 = new TextView(this);
					textView1.setText(""+i);
					TextView textView2 = new TextView(this);
					textView2.setText(""+loopsessiePunt.get(i).getTimestamp());
					TextView textView3 = new TextView(this);
					textView3.setText(berekenSnelheid(i) + " km/u");
					TextView textView4 = new TextView(this);
					textView4.setText(loopsessiePunt.get(i).getAfstand() + " m");
					tableRow[i].addView(textView1);
					tableRow[i].addView(textView2);
					tableRow[i].addView(textView3);
					tableRow[i].addView(textView4);
					tableLayout.addView(tableRow[i]);
				}
			}
		}
		dbHelper.close();
	}

	private int berekenSnelheid(final int i) {
		double afstand = loopsessiePunt.get(i).getAfstand();
		String tijd = loopsessiePunt.get(i).getTimestamp();
		String[] split = tijd.split(":");
		double hour = Double.parseDouble(split[0]);
		double min = Double.parseDouble(split[1]);
		double sec = Double.parseDouble(split[2]) + (hour * 3600) + (min * 60);
		int snelheid = (int) (afstand / sec * 3.6);
		return snelheid;
	}
}
